08. Write Your First Test

L5 A06 Write Your First Test (No TDD) Part 1 V2

Now it is is your turn to write your first tests.

Step 1: Create a Test Class

  1. Right click getActiveAndCompletedStats and select Generate then Test.

Then Create Test dialog opens.

  1. Change the Class name to StatisticsUtilsTest.
  2. Keep the rest of the defaults and press OK.

Then Choose Destination Directory dialog opens.

  1. Select the test directory (not androidTest) because you'll be writing local tests. Press OK.

Step 2: Write Your First Test

StatisticsUtilsTest.kt

class StatisticsUtilsTest {

    @Test
    fun getActiveAndCompletedStats_noCompleted_returnsHundredZero() {

        // Create an active tasks (the false makes this active)
        val tasks = listOf<Task>(
            Task("title", "desc", isCompleted = false)
        )
        // Call our function
        val result = getActiveAndCompletedStats(tasks)

        // Check the result
        assertEquals(result.completedTasksPercent, 0f)
        assertEquals(result.activeTasksPercent, 100f)
    }
}
  1. Go ahead and run the test (Right click StatisticsUtilTest and select Run).

Step 3: Write More Tests Yourself

Using the test above and the video, write the following tests:

  1. Start by writing test for when you have a normal task list:
    • If there is one completed task and no active tasks, the activeTasks percentage should be 0f, the completed tasks percentage should be 100f.
    • If there are two completed tests and three active test, the completed percentage should be 40f and the active percentage should be 60f.